<template>
  <div class="member-order-page">
    <XtxTabs v-model="activeName" @tab-click="clickTab">
      <XtxTabsPanel :name="item.name" :label="item.label" v-for="item in orderStatus" :key="item.name"></XtxTabsPanel>
    </XtxTabs>
    <!-- 订单列表 -->
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem v-for="item in orderList" :order="item" :key="item.id" @on-cancel="handlerCancel"
      @on-delete="handlerDelete" @on-confirm="handlerConfirm" @on-logistics="handlerLogistics"></OrderItem>
    </div>
  </div>
  <XtxPagination v-if="count > 0" :total="count" :pageSize="requestParams?.pageSize" :currentPage="requestParams?.page"
    @current-page="changePage" />
  <!-- 对于像：对话框，消息提示，确认提示，通知组件 适合使用传送门 Teleport -->
  <OrderCancel ref="orderCancelCom"></OrderCancel>
  <OrderLogistics ref="orderLogisticsCom"></OrderLogistics>
</template>
<script>
import { ref, reactive } from 'vue'
import { orderStatus } from '@/api/constants'
import OrderItem from './components/OrderItem.vue'
import { findOrderListAPI, delteOrderAPI, confirmOrderAPI } from '@/api/order'
import OrderCancel from './components/OrderCancel.vue'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
import OrderLogistics from './components/OrderLogistics.vue'

export default {
  name: 'MemberOrder',
  components: {
    OrderItem,
    OrderCancel,
    OrderLogistics
  },
  setup () {
    const activeName = ref('all')
    const count = ref(0)
    const loading = ref(false)
    const clickTab = ({ name, index }) => {
      requestParams.page = 1
      requestParams.orderState = index
      getOrderList()
    }
    // 查询订单参数
    const requestParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })
    // 订单列表
    const orderList = ref([])
    // 查询订单
    const getOrderList = () => {
      loading.value = true
      findOrderListAPI(requestParams).then(data => {
        orderList.value = data.result.items
        count.value = data.result.counts
      })
      loading.value = false
    }

    // 分页发生改变
    const changePage = (curentPage) => {
      requestParams.page = curentPage
      getOrderList()
    }

    const orderCancelCom = ref(null)
    // 取消订单
    const handlerCancel = (order) => {
      orderCancelCom.value.open(order)
    }

    // 删除商品
    const handlerDelete = (order) => {
      Confirm({ text: '您确认删除该条订单吗？' }).then(() => {
        delteOrderAPI([order.id]).then(() => {
          Message({ text: '删除订单成功', type: 'success' })
          getOrderList()
        })
      }).catch(e => {})
    }

    // 确认收货
    const handlerConfirm = (order) => {
      // item 就是你要确认收货的订单
      Confirm({ text: '您确认收到货吗？确认后货款将会打给卖家。' }).then(() => {
        confirmOrderAPI(order.id).then(() => {
          Message({ text: '确认收货成功', type: 'success' })
          // 确认收货后状态变成 待评价
          order.orderState = 4
        })
      })
    }

    const orderLogisticsCom = ref(null)
    // 查看物流
    const handlerLogistics = (order) => {
      orderLogisticsCom.value.open(order)
    }

    getOrderList()

    return {
      activeName,
      clickTab,
      orderStatus,
      orderList,
      changePage,
      count,
      loading,
      handlerCancel,
      orderCancelCom,
      handlerDelete,
      handlerConfirm,
      handlerLogistics,
      orderLogisticsCom
    }
  }
}
</script>
<style scoped lang="less">
.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
  min-height: 400px;
}

.order-item {
  margin-bottom: 20px;
  border: 1px solid #f5f5f5;

  .head {
    height: 50px;
    line-height: 50px;
    background: #f5f5f5;
    padding: 0 20px;
    overflow: hidden;

    span {
      margin-right: 20px;

      &.down-time {
        margin-right: 0;
        float: right;

        i {
          vertical-align: middle;
          margin-right: 3px;
        }

        b {
          vertical-align: middle;
          font-weight: normal;
        }
      }
    }

    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }

  .body {
    display: flex;
    align-items: stretch;

    .column {
      border-left: 1px solid #f5f5f5;
      text-align: center;
      padding: 20px;

      >p {
        padding-top: 10px;
      }

      &:first-child {
        border-left: none;
      }

      &.goods {
        flex: 1;
        padding: 0;
        align-self: center;

        ul {
          li {
            border-bottom: 1px solid #f5f5f5;
            padding: 10px;
            display: flex;

            &:last-child {
              border-bottom: none;
            }

            .image {
              width: 70px;
              height: 70px;
              border: 1px solid #f5f5f5;
            }

            .info {
              width: 220px;
              text-align: left;
              padding: 0 10px;

              p {
                margin-bottom: 5px;

                &.name {
                  height: 38px;
                }

                &.attr {
                  color: #999;
                  font-size: 12px;

                  span {
                    margin-right: 5px;
                  }
                }
              }
            }

            .price {
              width: 100px;
            }

            .count {
              width: 80px;
            }
          }
        }
      }

      &.state {
        width: 120px;

        .green {
          color: @xtxColor;
        }
      }

      &.amount {
        width: 200px;

        .red {
          color: @priceColor;
        }
      }

      &.action {
        width: 140px;

        a {
          display: block;

          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}

.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, .9) url(../../../assets/images/loading.gif) no-repeat center;
}

.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
